package com.cn.springcloudwechat.controller.personalcenter;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.common.utils.base.Const;
import com.cn.springcloudwechat.controller.vaccine.log.LogInfo;
import com.cn.springcloudwechat.controller.vaccine.log.OperateType;
import com.cn.springcloudwechat.model.dto.SignInDto;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserPointService;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserSignInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Lg
 * @Date 2018/8/6 9:47
 * @Content 用户签到
 */
@RestController
@RequestMapping("/web/userSignIn/")
public class UserSignInController {

    @Autowired
    YmUserSignInService ymUserSignInService;

    @Autowired
    YmUserPointService ymUserPointService;

    /**
     * 获取连续签到天数和今日是否已签到
     * @param ajaxRes
     * @return
     */
    @RequestMapping("getContinuousSignInDay")
    @LogInfo(operateType = OperateType.QUERY, content = "获取连续签到天数和今日是否已签到")
    public AjaxRes getContinuousSignInDay(AjaxRes ajaxRes){
        try {
           SignInDto signInDto = ymUserSignInService.getContinuousSignInDay();
           ajaxRes.setSucceed(signInDto, Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 用户签到
     * @return
     */
    @RequestMapping("insertUserSignIn")
    @LogInfo(operateType = OperateType.QUERY, content = "用户签到")
    public AjaxRes insertUserSignIn(AjaxRes ajaxRes){
        try {
            Integer result = ymUserSignInService.insertUserSignIn();
            if(result != 0){
                ymUserPointService.insertUserPoint(ThreadLocalUtil.getUser().getUserId(),"签到",null,"+5");
                SignInDto signInDto = ymUserSignInService.getContinuousSignInDay();
                if(signInDto.getSignInDays() % 7==0){
                    ymUserPointService.insertUserPoint(ThreadLocalUtil.getUser().getUserId(),"连续签到额外",null,"+50");
                }
            }
            ajaxRes.setSucceed(Const.SIGNIN_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    public static void main(String[] args) {
        Integer a = 21;
        System.out.println();
    }
}
